﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Action;
using Oak.Base;
using Oak.Model;
using Oak.MagSys.Logic;

namespace Oak.MagSys.Serv
{
    public class IconServ : OakServ<ISession>
    {
        private const string FN_INON = "iconId,parentId,frameId,iconImg,text,sortOrder";

        public IconServ(OakSession session) : base(session) { }

        public void List(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }
            MsIconQry qry = req.FirstModel<MsIconQry>();
            string msg;
            IconLG iconLG = new IconLG(Session);
            List<MsIconInfo> list = iconLG.GetIconList(qry, out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }
            res.AddResult("icons", FN_INON, list);
        }

        public void Add(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }
            MsIconInfo icon = req.FirstModel<MsIconInfo>();
            string msg;
            IconLG iconLG = new IconLG(Session);
            icon = iconLG.AddIcon(icon, out msg);
            if (icon == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("icons", FN_INON, icon);
        }

        public void Update(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }
            MsIconInfo icon = req.FirstModel<MsIconInfo>();
            string msg;
            IconLG iconLG = new IconLG(Session);
            icon = iconLG.UpdateIcon(icon, out msg);
            if (icon == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("icons", FN_INON, icon);
        }

        public void Delete(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }
            long iconId;
            if (!req.TryGetLong("iconId", out iconId))
            {
                res.Error("iconId参数错误");
                return;
            }

            string msg;
            IconLG iconLG = new IconLG(Session);
            if (!iconLG.DeleteIcon(iconId, out msg))
            {
                res.Error(msg);
                return;
            }
        }
    }
}
